package 数组;

public class L45 {

    public static void main(String[] args) {
        int[] nums = new int[]{2,3,1,1,4};
        System.out.println(new L45().jump(nums));
    }

    // 贪心算法
    public int jump(int[] nums) {
        if (nums.length == 1) return 0;
        int max = 0, step = 0, end = 0;     // 目前能跳的最远位置、跳跃次数、上次跳跃可达范围右边界
        for (int i = 0; i < nums.length - 1; i++) {
            max = Math.max(max, i + nums[i]);
            if (i == end) {
                end = max;
                step++;
            }
        }
        return step;
    }
}
